package hot_100;

public class 两数相加 {
    public static void main(String[] args) {

    }

    public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
        ListNode ans = new ListNode();
        ListNode n = ans;
        ListNode temp;
        int num = 0;
        while (l1 != null || l2 != null || num > 0) {
            int sum = num;
            if (l1 != null) {
                sum += l1.val;
                l1 = l1.next;
            }
            if (l2 != null) {
                sum += l2.val;
                l2 = l2.next;
            }
            num = sum / 10;
            sum = sum % 10;
            temp = new ListNode(sum);
            n.next = temp;
            n = temp;
        }
        return ans.next;
    }
/*    public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
        ListNode ans = new ListNode();
        ListNode n = ans;
        ListNode temp;
        int num = 0;
        while (l1 != null && l2 != null) {
            int sum = l1.val + l2.val + num;
            num = sum / 10;
            sum = sum % 10;
            temp = new ListNode(sum);
            n.next = temp;
            n = temp;
            l1 = l1.next;
            l2 = l2.next;
        }
        while (l1 != null) {
            int sum = l1.val + num;
            num = sum / 10;
            sum = sum % 10;
            temp = new ListNode(sum);
            n.next = temp;
            n = temp;
            l1 = l1.next;
        }
        while (l2 != null) {
            int sum = l2.val + num;
            num = sum / 10;
            sum = sum % 10;
            temp = new ListNode(sum);
            n.next = temp;
            n = temp;
            l2 = l2.next;
        }
        if (num > 0) {
            temp = new ListNode(num);
            n.next = temp;
        }
        return ans.next;
    }*/

    public class ListNode {
        int val;
        ListNode next;

        ListNode() {
        }

        ListNode(int val) {
            this.val = val;
        }

        ListNode(int val, ListNode next) {
            this.val = val;
            this.next = next;
        }
    }
}
